Search results in SharePoint 2010 offer great
opportunities for modification. Many can be done through the UI, but
some changes require a little more modification. This section offers
some insight into the possibilities through examples of how to add and
remove search result information and how to modify the behavior of
search results.
1. Adding Additional Info to Search Results with XSLT
When searching in
SharePoint using default search centers, only a small portion of the
available data is actually presented to the user. Often adding new
information is as easy as just modifying the XSLT template to display
another property value. Sometimes, however, some form of interactivity
with the search results is desired. This section focuses on how to
enrich the search result UI through an example with ratings.
2. Enabling Ratings
To get started, obviously
ratings needs to be turned on in at least one list or library. For this
example, a document library containing a number of PowerPoint
presentations and a few documents is used. To enable ratings, navigate
to the document library and choose "List settings" from the ribbon. From
the Document Library settings page, choose "Rating settings", which
opens a page where it is possible to enable or disable ratings as seen
in Figure 1.
Once this setting is made, a new column is added to the list and to the default view as seen next in Figure 2. At this point users can start rating content.
NOTE
When selecting a rating
and then doing a page reload, it can happen that the page is not
updated with the latest rating due to a delay in processing ratings in
SharePoint.
As you click the stars and add a
rating, this is logged by SharePoint through an asynchronous AJAX call.
An automated timer job in SharePoint named Social Data Maintenance Job
is responsible for calculating the average ratings. The ratings are
stored in the SocialRatings_Averages table. The rating information is computed from data in that table.
3. Making Ratings Searchable
To allow data (in this case,
ratings) to be searchable, it must be exposed as a searchable property.
More precisely, this is done by creating a managed property in search to
be included in the search results. To create the managed property, a
crawled property is required. When the properties are fully configured,
perform a full crawl for the properties to become available for
searching.
If you are uncertain if
these properties already exist, go to the Search Administration page in
Central Administration. There open the Metadata Properties link located
on the navigation bar to the left, and click the Crawled Properties
link. Search for the term "rating." as seen in Figure 3.
If it already exists, the result should show two hits, one for ows_AverageRating and another for ows_RatingCount.
If no hits are returned, make sure that rating is enabled on a document
library, start a full crawl in search, and wait for it to complete.
When the crawled properties exist, then add a managed property for each of the two crawled properties. Begin with the ows_AverageRating and create a managed property named "Rating." as seen in Figure 4. This value is of a decimal type, and choose the ows_AverageRating crawled property for the mapping.
Next, add a managed property named RatingCount the same way as with the Rating property. It should be mapped to the crawled property named ows_RatingCount
and have the type set to Integer. Ensure both of the crawled properties
are included in the index. This is done by selecting the "Include
values for this property in the search index" check box on each crawled
property. After all of these changes have been made, again do a full
crawl of search.
NOTE
An incremental crawl
does not update the index when changing managed and crawled properties.
When doing structural changes, a full crawl is required.
4. Adding Ratings to Search Results
When the managed properties are
added and a full crawl is completed, the XSL template must be modified
to show the rating in the search results Web Part. Open the search
results page that needs ratings added to it, and go to edit mode. Now
change the following three settings of the Display Properties on the
core search result Web Part.
Find the Use Location Visualization setting, and make sure it is unchecked.
Find the fetched properties. Here two new managed properties must be added. It should be expanded to contain these:
<Column Name="Rating"/>
<Column Name="RatingCount"/>
Find the XSL button and
click it. Update the XSL to include the ratings. An example of how it
could look is covered next. It is good practice to make a backup of the
existing XSL before editing it. This way it is easier to go back to
scratch in case the XSL gets wrongly edited.
For ease of use, it is
suggested to insert a call template block in the XSL template. For those
not too familiar with XSL, this is just a placeholder that displays the
XSL formatting template block to apply and which variables to use in
the template block. It can conceptually be understood as a method that
gets called.
Two variables are required:
Rating and Rating Count. These properties are exposed to the formatting
template block with the same names for easier reading. The formatting
template block is named DisplayRating.
<xsl:template name="DisplayRating">
<xsl:param name="rating" />
<xsl:param name="ratingcount" />
Rating: <xsl:value-of select="format-number($rating, '#.#')" />
(rated <xsl:value-of select="$ratingcount" /> times)
</xsl:template>
The next step is to include the required markup in the XSL template where the DisplayRating
template is to be rendered, and the conditions under which it should be
rendered. A suitable location in the layout for the rating to be
displayed is just before the hit highlighted summary.
Notice that a test is performed
to verify that the rating is higher than 0. This is only the case if
the item—in this case, documents—has a rating. If the document has a
rating, the DisplayRating template just shown is called and the rating text is rendered into the layout.
<div class="srch-Description2">
<xsl:if test="rating > 0">
<b>
<xsl:call-template name="DisplayRating">
<xsl:with-param name="rating" select="rating" />
<xsl:with-param name="ratingcount" select="ratingcount" />
</xsl:call-template>
</b><br />
</xsl:if>
<xsl:choose>
<xsl:when test="hithighlightedsummary[. != '']">
<xsl:call-template name="HitHighlighting">
<xsl:with-param name="hh" select="hithighlightedsummary" />